package tables

import (
	"strconv"

	"mini-admin/internal/conf"
	"mini-admin/internal/database"
	"mini-admin/internal/model"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/action"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

// GetSchedule return the model of table system_templates.
func GetSchedule(ctx *context.Context) (stTable table.Table) {

	stTable = table.NewDefaultTable(table.DefaultConfigWithDriver("postgresql"))

	info := stTable.GetInfo()
	info.AddField("ID", "id", db.Int).FieldSortable()
	info.AddField("关联ID", "ref_id", db.Int).FieldFilterable()
	info.AddField("任务类型", "job_type", db.Varchar).
		FieldDisplay(func(value types.FieldModel) interface{} {
			switch value.Value {
			case "commit_code":
				return "提交代码"
			case "commit_audit":
				return "提交审核"
			case "update_audit_status":
				return "更新审核状态"
			case "release":
				return "发布代码"
			case "sync_qr_code":
				return "同步二维码"
			default:
				return "未知状态"
			}
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Value: "commit_code", Text: "提交代码"},
			{Value: "commit_audit", Text: "提交审核"},
			{Value: "update_audit_status", Text: "更新审核状态"},
			{Value: "release", Text: "发布代码"},
			{Value: "sync_qr_code", Text: "同步二维码"},
		})
	info.AddField("任务业务数据", "job_data", db.Bit)
	info.AddField("状态", "job_status", db.Int).FieldDisplay(func(value types.FieldModel) interface{} {
		switch value.Row["job_status"].(int64) {
		case 0:
			return "初始化"
		case 1:
			return "执行中"
		case 2:
			return "执行失败"
		case 3:
			return "执行成功"
		default:
			return "未知状态"
		}
	}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Value: "0", Text: "初始化"},
			{Value: "1", Text: "执行中"},
			{Value: "2", Text: "执行失败"},
			{Value: "3", Text: "执行成功"},
		})
	info.AddField("重试次数", "retry_count", db.Int)
	info.AddField("下次执行时间", "next_executed_at", db.Datetime)

	info.AddActionButtonFront("任务重试", action.Ajax("/admin/retry_task",
		func(ctx *context.Context) (success bool, msg string, data interface{}) {
			scheduleID, _ := strconv.ParseInt(ctx.FormValue("id"), 10, 64)
			conn := database.New(conf.Conf.ORM)
			conn.Model(&model.Schedule{}).Where("id = ?", scheduleID).Updates(map[string]interface{}{
				"job_status": 0,
			})
			return true, "success", ""
		},
	))

	info.SetTable("schedule").SetTitle("定时任务").SetDescription("定时任务表")

	formList := stTable.GetForm()
	formList.AddField("ID", "id", db.Int, form.Default).FieldDisplayButCanNotEditWhenUpdate().FieldDisableWhenCreate()
	formList.AddField("关联ID", "ref_id", db.Int, form.Number)
	formList.AddField("任务类型", "job_type", db.Varchar, form.SelectSingle).FieldOptions(types.FieldOptions{
		{Text: "提交代码", Value: "commit_code"},
		{Text: "提交审核", Value: "commit_audit"},
		{Text: "更新审核状态", Value: "update_audit_status"},
		{Text: "发布代码", Value: "release"},
		{Text: "同步二维码", Value: "sync_qr_code"},
	})
	formList.AddField("任务业务数据", "job_data", db.Bit, form.TextArea)
	formList.AddField("重试次数", "retry_count", db.Int, form.Number)
	formList.AddField("下次执行时间", "next_executed_at", db.Datetime, form.Datetime)

	formList.SetTable("schedule").SetTitle("定时任务").SetDescription("定时任务表")

	return
}
